package cloud.demarcia.crawler3.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;

public class Browser {
    private static final WebClient webClient = new WebClient(BrowserVersion.CHROME);


    static{
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.waitForBackgroundJavaScript(2000);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX
    }

    public static HtmlPage browse(String urlString) throws IOException, InterruptedException {
        HtmlPage page=webClient.getPage(urlString);
        Thread.sleep(2000);
        return page;
    }
    public static HtmlPage browse(URL url) throws IOException, InterruptedException {
        HtmlPage page=webClient.getPage(url);
        Thread.sleep(2000);
        return page;
    }

    public static void shutdown(){
        webClient.close();
    }


}
